package com.cqut.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cqut.model.ComProcess;
import com.cqut.dao.ComProcessMapper;
import com.cqut.dao.ComProcessMapperExtra;
import com.cqut.dto.LimitShowDTO;
import com.cqut.service.IComProcessService;
import com.cqut.util.SqlUtil;

@Service
public class ComProcessService implements IComProcessService {
    @Resource(name="comProcessMapper")
    private ComProcessMapper comProcessMapper;

    @Resource(name="comProcessMapperExtra")
    private ComProcessMapperExtra comProcessMapperExtra;
    
    @Override
    public boolean save(ComProcess comProcess) {
        return comProcessMapper.insert(comProcess) > 0;
    }

    @Override
    public boolean update(ComProcess comProcess) {
        return comProcessMapper.updateByPrimaryKeySelective(comProcess) >= 0;
    }

    @Override
    public boolean delete(String comProcessId) {
        return comProcessMapper.deleteByPrimaryKey(comProcessId) > 0;
    }

    @Override
    public boolean batchDelete(String[] primaryKeys) {
        return comProcessMapper.deleteByPrimaryKeys(primaryKeys) > 0;
    }

    @Override
    public ComProcess findModel(String comProcessId) {
        return comProcessMapper.selectByPrimaryKey(comProcessId);
    }

    @Override
    public ComProcess findModel(String comProcessId, String[] parameters) {
        return comProcessMapper.findModel(comProcessId, SqlUtil.formatParameters(parameters));
    }

    public Map<String, Object> findMap(String comProcessId) {
        String[][] parameters = {{"com_process_id", "comProcessId"}, {"com_process_name", "comProcessName"}, {"start_time", "startTime"}, {"end_time", "endTime"}, {"is_get_pro", "isGetPro"}, {"competition_type", "competitionType"}, {"least_number", "leastNumber"}, {"max_number", "maxNumber"}};
        return this.findMap(comProcessId, parameters);
    }

    public Map<String, Object> findMap(String comProcessId, String[][] parameters) {
        return comProcessMapper.findMap(comProcessId, SqlUtil.formatParametersForAlias(parameters));
    }

    @Override
    public List<ComProcess> loadModels() {
        return this.loadModels(null, null, null, null, -1, -1);
    }

    @Override
    public List<ComProcess> loadModels(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return this.loadModels(null, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<ComProcess> loadModels(String[] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return comProcessMapper.loadModels(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps() {
        String[][] parameters = {{"com_process_id", "comProcessId"}, {"com_process_name", "comProcessName"}, {"start_time", "startTime"}, {"end_time", "endTime"}, {"is_get_pro", "isGetPro"}, {"competition_type", "competitionType"}, {"least_number", "leastNumber"}, {"max_number", "maxNumber"}};
        return this.loadMaps(parameters, null, null, null, null, -1, -1);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        String[][] parameters = {{"com_process_id", "comProcessId"}, {"com_process_name", "comProcessName"}, {"start_time", "startTime"}, {"end_time", "endTime"}, {"is_get_pro", "isGetPro"}, {"competition_type", "competitionType"}, {"least_number", "leastNumber"}, {"max_number", "maxNumber"}};
        return this.loadMaps(parameters, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String[][] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return comProcessMapper.loadMaps(SqlUtil.formatParametersForAlias(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public long count() {
        return this.count(null, false);
    }

    @Override
    public long count(String condition, Object[] values) {
        return this.count(null, condition, values, false);
    }

    @Override
    public long count(String[] parameters, boolean isDistinct) {
        return this.count(parameters, null, null, isDistinct);
    }

    @Override
    public long count(String[] parameters, String condition, Object[] values, boolean isDistinct) {
        return comProcessMapper.count(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), isDistinct);
    }

	@Override
	public List<Map<String, Object>> findStage(String competitionId, String comProcessName, LimitShowDTO limitShowDTO) {
		return comProcessMapperExtra.findStage(competitionId, comProcessName, limitShowDTO.calculateStart(), limitShowDTO.calculateEnd());
	}

	@Override
	public long getCountByComProcessName(String competitionId, String comProcessName) {
		return comProcessMapperExtra.getCountByComProcessName(competitionId, comProcessName);
	}

	@Override
	public Map<String, Object> comProcessObject(String comProcessId) {
		return comProcessMapperExtra.comProcessObject(comProcessId);
	}
	
}
